<template>
  <div class="login">
    <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
      <div style="text-align: center">
        <img src="../assets/images/JHHJ.png" />
      </div>
      <el-form-item prop="username">
        <el-input
          :prefix-icon="User"
          size="large"
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="用户名"
          @keyup.enter.native="handleLogin"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          show-password
          :prefix-icon="Lock"
          size="large"
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        ></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input
          style="width: 60%"
          size="large"
          v-model="loginForm.code"
          type="text"
          autocomplete="off"
          placeholder="验证码"
          @keyup.enter.native="handleLogin"
        ></el-input>
        <img
          v-if="captchaOpen"
          :src="imgSrc"
          @click="getCode"
          style="width: 36%; height: 38px;margin-left: 10px"
        />
      </el-form-item>
      <div style="display: flex;justify-content: space-between;">
        <span>记住我</span>
        <el-switch v-model="loginForm.remember" size="large"></el-switch>
      </div>
      <el-form-item>
        <el-button
          :loading="loading"
          size="large"
          type="primary"
          style="width:100%; margin:10px 0 10px 0"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div>
          <router-link class="link-type" :to="'/login'">忘记密码</router-link>
        </div>
        <div style="margin-left: auto;">
          <router-link class="link-type" :to="'/login'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { User, Lock } from "@element-plus/icons-vue";
import { ref, reactive, onMounted } from "vue";
import { useRouter } from "vue-router";
import store from "@/store";
import { getCaptcha } from "@/api/login";
import Cookies from "js-cookie";
import { decrypt, encrypt } from "@/utils/jsencrypt";

const Router = useRouter();
const loginFormRef = ref();
const loading = ref(false);
const captchaOpen = ref(false);
const imgSrc = ref();

let loginForm = ref({
  username: "",
  password: "",
  code: "",
  uuid: "",
  remember: false
});

//表单校验规则
const loginRules = reactive({
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "blur", message: "请输入验证码" }]
});

//组件被挂载后执行
onMounted(() => {
  getCode();
  getCookie();
});

//获取验证码
const getCode = () => {
  getCaptcha().then(res => {
    loginForm.value.uuid = res.uuid;
    imgSrc.value = "data:image/png;base64," + res.img;
    captchaOpen.value = true;
  });
};

//获取cookie中的用户信息
const getCookie = () => {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const remember = Cookies.get("remember");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password:
      password === undefined ? loginForm.value.password : decrypt(password),
    remember: remember === undefined ? loginForm.value.remember : remember
  };
};

//登录方法
const handleLogin = () => {
  loginFormRef.value.validate(async valid => {
    if (valid) {
      loading.value = true;
      if (loginForm.value.remember === true) {
        //设置用户信息保留30天
        Cookies.set("username", loginForm.value.username, { expires: 30 });
        Cookies.set("password", encrypt(loginForm.value.password), {
          expires: 30
        });
        Cookies.set("remember", loginForm.value.remember, { expires: 30 });
      } else {
        //删除用户信息Cookie
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("remember");
      }
      store
        .dispatch("user/Login", loginForm.value)
        .then(() => {
          //如果是在别的页面重定向过来,登录成功后跳转到之前的页面,否则跳转到首页
          const redirect = Router.currentRoute.value.query.redirect || "/";
          Router.push({ path: redirect });
        })
        .catch(() => {
          loading.value = false;
          getCode();
        });
    }
  });
};
</script>

<style lang="scss">
* {
  margin: 0;
  padding: 0;
}

.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 0 25px 5px 25px;

  el-input {
    height: 50px;
    padding-top: 10px;

    input {
      height: 50px;
    }
  }
}
</style>
